package com.yc.boot.work.business;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.boot.common.model.response.Outcome;
import com.yc.boot.work.entity.TestClass;
import com.yc.boot.work.entity.TestStudent;
import com.yc.boot.work.model.TestClassStudent;
import com.yc.boot.work.model.find.FindClassPage;
import com.yc.boot.work.model.save.SaveClass;
import com.yc.boot.work.model.update.UpdateClass;
import com.yc.boot.work.service.TestClassService;
import com.yc.boot.work.service.TestStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.yc.boot.common.constant.BaseConstant.MYSQL_LIMIT_ONE;
import static com.yc.boot.common.constant.BaseConstant.ZERO;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2021/12/14/15:05
 * @Description:
 */
@Slf4j
@Service
public class TestClassBussiness {
    @Resource
    private TestClassService testClassService;

    @Resource
    private TestStudentService testStudentService;



    /*
     * @Description  查询班级表和学生表的所有信息
     * @Param
     * @return List<TestClassStudent>
     **/
    public List<TestClassStudent> queryClassAndStudent(){
        List<TestClass> classMassage = testClassService.list();
        List<TestClassStudent> classAndStudent = Convert.toList(TestClassStudent.class,classMassage);
        List<TestClassStudent> classAndStudentinfo =new ArrayList<>();
        for(TestClassStudent classes: classAndStudent ){
            TestClassStudent cs= new TestClassStudent();
            List <TestStudent> students=testStudentService.list(new QueryWrapper<TestStudent>().lambda()
            .eq(TestStudent::getClassId,classes.getId())
            );
            cs.setId(classes.getId());
            cs.setName(classes.getName());
            cs.setNumber(classes.getNumber());
            cs.setStudent(students);
            classAndStudentinfo.add(cs);
        }
        return classAndStudentinfo;

    }

    /*
     * @Description 根据名字精确查询班级信息
     * @Param
     * @return
     **/
    public TestClass queryByNameOcc(String name){

          TestClass classmessage= testClassService.getOne(new QueryWrapper<TestClass>().lambda().eq(StrUtil.isNotBlank(name),TestClass::getName,name).last(MYSQL_LIMIT_ONE));
          return classmessage;
    }

    /*
     * @Description 添加班级单条信息
     * @Param
     * @return
     **/

    public Boolean addClassOne(SaveClass classmessage) {
        TestClass classOne = Convert.convert(TestClass.class, classmessage);
        classOne.setCreateBy(1L);
        return testClassService.save(classOne);
    }

    /*
     * @Description 根据ID和班级名查询班级信息并分页
     * @Param
     * @return
     **/
    public IPage<TestClass> queryClass(FindClassPage param){
        IPage<TestClass>  queryclass =testClassService.page(new Page<>(param.getCurrent(), param.getSize()),
                new QueryWrapper<TestClass>().lambda()
                .eq(ObjectUtil.isNotNull(param.getId()),TestClass::getId,param.getId())
                .like(StrUtil.isNotBlank(param.getName()),TestClass::getName,param.getName())
                .orderByAsc(TestClass::getId)
        );
        return queryclass;
    }


    /*
     * @Description 修改班级信息（班级名称和班级人数）
     * @Param
     * @return
     **/

    public Outcome updateClass(UpdateClass classz){
       TestClass newclass =Convert.convert(TestClass.class,classz);
       TestClass oldclass =testClassService.getById(classz.getId());
        if(ObjectUtil.isEmpty(oldclass)){
            return Outcome.failure("相应的班级不存在!");
        }
        if(ObjectUtil.isNotNull(classz.getName())&&testClassService.count(
                new QueryWrapper<TestClass>().lambda()
                        .eq(TestClass::getName,classz.getName()).ne(TestClass::getId,classz.getId()))>ZERO)
        {
            return Outcome.failure("班级名已经被占用!");
        }
        Boolean classs =testClassService.updateById(newclass);

        return Outcome.status(classs);


    }

    /*
     * @Description   删除班级信息
     * @Param
     * @return
     **/
    public Boolean delClass(List<Long> id){
        Boolean updateClass =testClassService.updateBatchById(Convert.toList(TestClass.class,id.stream()
                .map(idz -> new TestClass().setUpdateBy(1L).setId(idz)).collect(Collectors.toList())));
        Boolean delClass =testClassService.removeByIds(id);
    return updateClass&&delClass;
    }
}
